package com.laborder.sysyy.controller.course;


import com.laborder.sysyy.entity.Announce;
import com.laborder.sysyy.entity.Course;
import com.laborder.sysyy.entity.Labs;
import com.laborder.sysyy.entity.User;
import com.laborder.sysyy.service.course.CourseService;
import com.laborder.sysyy.service.labs.LabsService;
import com.laborder.sysyy.utils.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "course")
public class CourseController {
	@Autowired
	private CourseService courseService;
	@Autowired
	private LabsService labsService;

	@GetMapping("toCourse")
	public String toCourse(Model model){

		return "/course/courseList";
	}
	@GetMapping("toAddCourse")
	public String toAddCourse(Model model){
		List<Labs> labs = labsService.getAllLabs();
		model.addAttribute("labsList",labs);
		return "/course/courseAdd";
	}
    @ResponseBody
	@RequestMapping("findCourseWithPage")
	public Map<String,Object> findCourseWithPage(int page,int limit,String clName){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("page", (page-1)*limit);
		map.put("rows", limit);
		map.put("cName", clName);
		Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count",courseService.getCourseCount(map));
        resultMap.put("data",courseService.findCourseWithPage(map));
		return resultMap;
	}
    @ResponseBody
	@RequestMapping("saveCourseInfo")
	public  int saveCourseInfo(Course course) {
	    int flag = 0;

        Course c = courseService.getCourseInfoByName(course.getcName());
        if(c!=null){
            flag = -1;
        }else if(c==null){
            course.setAddDate(DateUtils.getDateTime());
            course.setUpdateDate(DateUtils.getDateTime());
            flag = courseService.saveCourseInfo(course);
        }else{
            flag = 0;
        }
		return flag;
	}

	@GetMapping("editCourse")
	public String editCourse(Model model,String cId) {
		List<Labs> labs = labsService.getAllLabs();
		Course course = courseService.getCourseInfoById(cId);
		model.addAttribute("labsList",labs);
		model.addAttribute("course",course);
		return "/course/courseEdit";
	}
	 /**
	   * @function:  课程信息修改
	   * @author: 759057893@qq.com lyz
	   * @date: 2019/1/16 16:18
	   */
	@ResponseBody
	@RequestMapping("updateCourseInfo")
	public int updateCourseInfo(Course course,String cId) {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User)session.getAttribute("user");
		course.setAddUser(user.getUserName());
		Course c =courseService.getCourseInfoById(cId);
		course.setAddDate(c.getAddDate());
		course.setUpdateDate(DateUtils.getDateTime());
		return courseService.updateCourseInfo(course);
	}

	@ResponseBody
	@RequestMapping("delCourseInfo")
	public int delCourseInfo(int cId) {
		int flag = 0;
		flag = courseService.delCourseInfo(cId);

		return flag;
	}
	@ResponseBody
	@RequestMapping("delManyCourseInfo")
	public 	 int delManyCourseInfo(@RequestParam("cIds[]") int cIds[]) {
		int flag = 0;
		for(Integer cId:cIds){
			flag = courseService.delCourseInfo(cId);
		}
		return flag;
	}

	@RequestMapping("getCourseInfoById")
	public  Course getCourseInfoById(String cId) {
		return courseService.getCourseInfoById(cId);
	}
	
	
}